package com.erp.base.business.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.erp.base.base.CrudService;
import com.erp.base.business.dao.AppUserDao;
import com.erp.base.business.dao.ExpertApprovalRecordDao;
import com.erp.base.business.dao.ExpertUserDao;
import com.erp.base.business.entity.AppUser;
import com.erp.base.business.entity.ExpertApprovalRecord;
import com.erp.base.business.entity.ExpertUser;
import com.erp.base.common.utils.UserUtils;



@Service
public class ExpertApprovalRecordService extends CrudService<ExpertApprovalRecordDao, ExpertApprovalRecord, Long> {
	@Autowired
	private AppUserDao appUserDao;
	@Autowired
	private ExpertUserDao expertUserDao;
	
	public ExpertApprovalRecord getApplyStatus(Long userId) {
		return dao.getApplyStatus(userId);
	}
	
	public void insert(ExpertApprovalRecord entity ){
		dao.insert(entity);
	}
	
	public void update(ExpertApprovalRecord entity ){
		dao.update(entity);
	}

	public void updateExpert(ExpertApprovalRecord expertApprovalRecord) {
		Long userId=expertApprovalRecord.getUserId();
		//更新审批结果
		expertApprovalRecord.setUpdateBy(UserUtils.getUser().getName());
		expertApprovalRecord.setCreateDate(new Date());
		update(expertApprovalRecord);
		//获取专家对应数据，修改状态
		ExpertUser expertUser= expertUserDao.findByUserId(userId);
		expertUser.setStatus(expertApprovalRecord.getStatus());
		expertUser.setUpdateBy(UserUtils.getUser().getName());
		expertUser.setCreateDate(new Date());
		//如果审批通过，则将用户表状态的专家标志设置为1
		if("1".equals(expertApprovalRecord.getStatus())){
			AppUser appuser=appUserDao.get(userId);
			appuser.setIsExpert("1");
			appuser.setUpdateBy(UserUtils.getUser().getName());
			appuser.setUpdateDate(new Date());
			appuser.setRemarks("用户申请成为专家审批通过！");
			appUserDao.update(appuser);
			expertUser.setRemarks("用户申请成为专家审批通过！");
		}
		expertUserDao.update(expertUser);
	}
}
